<?php
/**
 * Created by PhpStorm.
 * User: trisatria
 * Date: 1/7/14
 * Time: 11:04 AM
 */

namespace Admin\Model;


use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Insert;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Sql;
use Zend\Db\ResultSet\HydratingResultSet;

class AdminCategoriesTable
{
    protected $tableGateway;
    protected $adapter;
    protected $sql;

    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }

    public function getAll(){
        $rs = $this->fetchAll();
        $arr = array();
        foreach($rs as $key => $val){
            $arr[] = $val;
        }
        return $arr;
    }

    public function fetchAll($paginated=false)
    {
        if($paginated) {
            $select = new Select('topic_cat');
            $select->order('sort_order ASC');
            // create a new result set based on the Album entity
            $resultSetPrototype = new ResultSet();
            $resultSetPrototype->setArrayObjectPrototype(new AdminCategories());
            // create a new pagination adapter object
            $paginatorAdapter = new DbSelect(
            // our configured select object
                $select,
                // the adapter to run it against
                $this->tableGateway->getAdapter(),
                // the result set to hydrate
                $resultSetPrototype
            );
            $paginator = new Paginator($paginatorAdapter);
            return $paginator;
        }
            $resultSet = $this->tableGateway->select(function(Select $select){
                $select->order('sort_order ASC');});
            return $resultSet;



      /*  $resultSet = $this->tableGateway->select();
        return $resultSet; */

    }


    public function getCategories($id)
    {
        $id  = (int) $id;
        $rowset = $this->tableGateway->select(array('id' => $id));
        $row = $rowset->current();
        if (!$row) {
            throw new \Exception("Could not find row $id");
        }
        return $row;
    }
    public function getCateByParentId($parentId){
        $parentId  = (int) $parentId;
        $rowset = $this->tableGateway->select(array('cat_parent' => $parentId));
        $row = $rowset->current();
        if (!$row) {
            throw new \Exception("Could not find row cat_parent : $parentId");
        }
        return $row;
    }

    public function saveCategories(AdminCategories $categories)
    {
        $data = array(
            'cat_parent' => $categories->cat_parent,
            'sort_order' => $categories->sort_order,
            'title'  => $categories->title,
        );
        $id = (int)$categories->id;
        if ($id == 0) {
            $this->tableGateway->insert($data);
        } else {
            if ($this->getCategories($id)) {
                $this->tableGateway->update($data, array('id' => $id));
            } else {
                throw new \Exception('Form id does not exist');
            }
        }
    }

    public function deleteCategories($id)
    {
        $this->tableGateway->delete(array('id' => $id));
    }



    public function getCategoryBy($parentId = null)
    {

        $resultSet = $this->tableGateway->select(array('cat_parent'=>$parentId));

        return $resultSet;
    }


    //draw child
    function DrawCategory($cat_parent = 0,$prefix = ''){
        $html = "";
        $prefix = '';
        $data = $this->fetchAll();
        $disable = '';
        $href= '#myModal';
        foreach($data as $item){
            if($item->cat_parent == $cat_parent){
                $id = $item->id;
                if($id ==1) {
                    $disable = 'disabled="disabled"';
                    $href = '';
                }

                $html .= ' <tr>
                            <td>'. $item->title .'</td>
                            <td class="center">'. $item->user_id .'</td>

                            <td class="center">
                                <a class="btn btn-success edit" dataId="'.$item->id .'">
                                    <i class="icon-edit icon-white"></i>
                                    Edit
                                </a>

                                <a '.$disable.' class="btn btn-danger del" dataId="'.$item->id .'" href="'.$href.'" role="button" data-toggle="modal" >
                                    <i class="icon-trash icon-white"></i>
                                    Delete
                                </a>
                            </td>
                        </tr>';
                $html .= $this->DrawCategory($id, $prefix );
            }
        }
        return $html;

    }
}